package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.bean.dingdanBean;
import com.bean.huojiaBean;
import com.util.DBUtil;

public class dingdanDao {
		
	
	public static boolean add(String u,String useraddA,String useraddB) {//添加订单
		Connection conn=DBUtil.getConnectDb();
		String sql = "INSERT INTO dingdan (dd_account, dd_commodity, dd_number) VALUES (?,?,?)";
		int rs;
		PreparedStatement stm = null;
		try {
		stm = conn.prepareStatement(sql);
		stm.setString(1,u);
		stm.setString(2,useraddA);
		stm.setString(3,useraddB);
		
		boolean a=false;
		rs = stm.executeUpdate();
		if(rs==1){
			return true;
			} else {
			return false;
			} 
		} catch (SQLException e) {
			return false;
		}
	}
	
	
	
	
	
	public static boolean delete(String dd_order,String dd_account) {//用户个人删除订单
		Connection conn=DBUtil.getConnectDb();
		String sql = "delete from dingdan where dd_order=? AND dd_account=? AND stat = '未出库'";
		int rs;
		PreparedStatement stm = null;
		try {
		stm = conn.prepareStatement(sql);
		stm.setString(1,dd_order);
		stm.setString(2,dd_account);
		
		boolean a=false;
		rs = stm.executeUpdate();
		if(rs==1){
			return true;
			} else {
			return false;
			} 
		} catch (SQLException e) {
			return false;
		}
	}
	
	
	
	
	public static ArrayList<dingdanBean> getLikeList(String dd_account) {//查询用户个人的全部订单 存放多个订单信息的集合
		
		ArrayList<dingdanBean> tag_Array = new ArrayList<dingdanBean>();//建立一个数组集合
		Connection conn=DBUtil.getConnectDb();
		String sql = "select *from dingdan where dd_account=? ORDER BY dd_order DESC";
		PreparedStatement stm = null;
		ResultSet rs = null;
		
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1,dd_account);
			rs = stm.executeQuery();
			while(rs.next()) {//读取数据库
				dingdanBean tag = new dingdanBean();//查询商品 存放单个订单信息
				tag.setDd_order(rs.getString("dd_order"));
				tag.setDd_account(rs.getString("dd_account"));
				tag.setDd_commodity(rs.getString("dd_commodity"));
				tag.setDd_number(rs.getString("dd_number"));
				tag.setTotal_price(rs.getString("total_price"));
				tag.setStat(rs.getString("stat"));
				tag_Array.add(tag);
			}
			rs.close();
			stm.close();
			
			
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return tag_Array;
	}
	
	
	
	
	
	public static ArrayList<dingdanBean> getLikeList(String dd_order,String dd_account) {//查询订单 存放多个订单信息的集合
			
			ArrayList<dingdanBean> tag_Array = new ArrayList<dingdanBean>();//建立一个数组集合
			Connection conn=DBUtil.getConnectDb();
			String sql = "select *from dingdan where dd_order =? AND dd_account=?";
			PreparedStatement stm = null;
			ResultSet rs = null;
			
			try {
				stm = conn.prepareStatement(sql);
				stm.setString(1,dd_order);
				stm.setString(2,dd_account);
				rs = stm.executeQuery();
				while(rs.next()) {//读取数据库
					dingdanBean tag = new dingdanBean();//查询订单 存放单个订单信息
					tag.setDd_order(rs.getString("dd_order"));
					tag.setDd_account(rs.getString("dd_account"));
					tag.setDd_commodity(rs.getString("dd_commodity"));
					tag.setDd_number(rs.getString("dd_number"));
					tag.setTotal_price(rs.getString("total_price"));
					tag.setStat(rs.getString("stat"));
					tag_Array.add(tag);
				}
				rs.close();
				stm.close();
				
				
				} catch (SQLException e) {
					e.printStackTrace();
				}
			return tag_Array;
		}
	
	
	
	
	
	
	
	public static ArrayList<dingdanBean> getLikeListDd_order() {//通过订单号查询全部订单 存放多个订单信息的集合
		
		ArrayList<dingdanBean> tag_Array = new ArrayList<dingdanBean>();//建立一个数组集合
		Connection conn=DBUtil.getConnectDb();
		String sql = "select *from dingdan ORDER BY dd_order";
		PreparedStatement stm = null;
		ResultSet rs = null;
		
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()) {//读取数据库
				dingdanBean tag = new dingdanBean();//查询商品 存放单个订单信息
				tag.setDd_order(rs.getString("dd_order"));
				tag.setDd_account(rs.getString("dd_account"));
				tag.setDd_commodity(rs.getString("dd_commodity"));
				tag.setDd_number(rs.getString("dd_number"));
				tag.setTotal_price(rs.getString("total_price"));
				tag.setStat(rs.getString("stat"));
				tag_Array.add(tag);
			}
			rs.close();
			stm.close();
			
			
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return tag_Array;
	}
	
	
	
	
	public static ArrayList<dingdanBean> getLikeListDd_order(String dd_order) {//通过订单号查询全部订单 存放多个订单信息的集合
			
			ArrayList<dingdanBean> tag_Array = new ArrayList<dingdanBean>();//建立一个数组集合
			Connection conn=DBUtil.getConnectDb();
			String sql = "select *from dingdan where dd_order=?";
			PreparedStatement stm = null;
			ResultSet rs = null;
			
			try {
				stm = conn.prepareStatement(sql);
				stm.setString(1,dd_order);
				rs = stm.executeQuery();
				while(rs.next()) {//读取数据库
					dingdanBean tag = new dingdanBean();//查询商品 存放单个订单信息
					tag.setDd_order(rs.getString("dd_order"));
					tag.setDd_account(rs.getString("dd_account"));
					tag.setDd_commodity(rs.getString("dd_commodity"));
					tag.setDd_number(rs.getString("dd_number"));
					tag.setTotal_price(rs.getString("total_price"));
					tag.setStat(rs.getString("stat"));
					tag_Array.add(tag);
				}
				rs.close();
				stm.close();
				
				
				} catch (SQLException e) {
					e.printStackTrace();
				}
			return tag_Array;
		}
	
	
	
	public static ArrayList<dingdanBean> getLikeListDd_account() {//通过订单号查询全部订单 存放多个订单信息的集合
		
		ArrayList<dingdanBean> tag_Array = new ArrayList<dingdanBean>();//建立一个数组集合
		Connection conn=DBUtil.getConnectDb();
		String sql = "select *from dingdan ORDER BY dd_account";
		PreparedStatement stm = null;
		ResultSet rs = null;
		
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()) {//读取数据库
				dingdanBean tag = new dingdanBean();//查询商品 存放单个订单信息
				tag.setDd_order(rs.getString("dd_order"));
				tag.setDd_account(rs.getString("dd_account"));
				tag.setDd_commodity(rs.getString("dd_commodity"));
				tag.setDd_number(rs.getString("dd_number"));
				tag.setTotal_price(rs.getString("total_price"));
				tag.setStat(rs.getString("stat"));
				tag_Array.add(tag);
			}
			rs.close();
			stm.close();
			
			
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return tag_Array;
	}
	
	
	public static ArrayList<dingdanBean> getLikeListDd_account(String dd_account) {//通过用户名查询全部订单 存放多个订单信息的集合
		
		ArrayList<dingdanBean> tag_Array = new ArrayList<dingdanBean>();//建立一个数组集合
		Connection conn=DBUtil.getConnectDb();
		String sql = "select *from dingdan where dd_account=? ORDER BY dd_order";
		PreparedStatement stm = null;
		ResultSet rs = null;
		
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1,dd_account);
			rs = stm.executeQuery();
			while(rs.next()) {//读取数据库
				dingdanBean tag = new dingdanBean();//查询商品 存放单个订单信息
				tag.setDd_order(rs.getString("dd_order"));
				tag.setDd_account(rs.getString("dd_account"));
				tag.setDd_commodity(rs.getString("dd_commodity"));
				tag.setDd_number(rs.getString("dd_number"));
				tag.setTotal_price(rs.getString("total_price"));
				tag.setStat(rs.getString("stat"));
				tag_Array.add(tag);
			}
			rs.close();
			stm.close();
			
			
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return tag_Array;
	}
	
	
	
	
	public static ArrayList<dingdanBean> getLikeListDd_commodity() {//通过商品名查询全部订单 存放多个订单信息的集合
		
		ArrayList<dingdanBean> tag_Array = new ArrayList<dingdanBean>();//建立一个数组集合
		Connection conn=DBUtil.getConnectDb();
		String sql = "select *from dingdan ORDER BY dd_commodity";
		PreparedStatement stm = null;
		ResultSet rs = null;
		
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()) {//读取数据库
				dingdanBean tag = new dingdanBean();//查询商品 存放单个订单信息
				tag.setDd_order(rs.getString("dd_order"));
				tag.setDd_account(rs.getString("dd_account"));
				tag.setDd_commodity(rs.getString("dd_commodity"));
				tag.setDd_number(rs.getString("dd_number"));
				tag.setTotal_price(rs.getString("total_price"));
				tag.setStat(rs.getString("stat"));
				tag_Array.add(tag);
			}
			rs.close();
			stm.close();
			
			
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return tag_Array;
	}
	
	
	public static ArrayList<dingdanBean> getLikeListDd_commodity(String dd_commodity) {//通过商品名查询全部订单 存放多个订单信息的集合
		
		ArrayList<dingdanBean> tag_Array = new ArrayList<dingdanBean>();//建立一个数组集合
		Connection conn=DBUtil.getConnectDb();
		String sql = "select *from dingdan where dd_commodity=? ORDER BY dd_order";
		PreparedStatement stm = null;
		ResultSet rs = null;
		
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1,dd_commodity);
			rs = stm.executeQuery();
			while(rs.next()) {//读取数据库
				dingdanBean tag = new dingdanBean();//查询商品 存放单个订单信息
				tag.setDd_order(rs.getString("dd_order"));
				tag.setDd_account(rs.getString("dd_account"));
				tag.setDd_commodity(rs.getString("dd_commodity"));
				tag.setDd_number(rs.getString("dd_number"));
				tag.setTotal_price(rs.getString("total_price"));
				tag.setStat(rs.getString("stat"));
				tag_Array.add(tag);
			}
			rs.close();
			stm.close();
			
			
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return tag_Array;
	}
	
	
	
	public static ArrayList<dingdanBean> getLikeListStat0() {//查询全部未出库订单 存放多个订单信息的集合
		
		ArrayList<dingdanBean> tag_Array = new ArrayList<dingdanBean>();//建立一个数组集合
		Connection conn=DBUtil.getConnectDb();
		String sql = "select *from dingdan where stat = '未出库' ORDER BY dd_order";
		PreparedStatement stm = null;
		ResultSet rs = null;
		
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()) {//读取数据库
				dingdanBean tag = new dingdanBean();//查询商品 存放单个订单信息
				tag.setDd_order(rs.getString("dd_order"));
				tag.setDd_account(rs.getString("dd_account"));
				tag.setDd_commodity(rs.getString("dd_commodity"));
				tag.setDd_number(rs.getString("dd_number"));
				tag.setTotal_price(rs.getString("total_price"));
				tag.setStat(rs.getString("stat"));
				tag_Array.add(tag);
			}
			rs.close();
			stm.close();
			
			
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return tag_Array;
	}
	
	
//	
//	public static ArrayList<dingdanBean> getLikeListStat0(String dd_order) {//查询全部未出库订单 存放多个订单信息的集合
//		
//		ArrayList<dingdanBean> tag_Array = new ArrayList<dingdanBean>();//建立一个数组集合
//		Connection conn=DBUtil.getConnectDb();
//		String sql = "UPDATE dingdan SET stat = '未出库' WHERE dd_order = ? AND stat = '已出库'";
//		PreparedStatement stm = null;
//		ResultSet rs = null;
//		
//		try {
//			stm = conn.prepareStatement(sql);
//			stm.setString(1,dd_order);
//			rs = stm.executeQuery();
//			while(rs.next()) {//读取数据库
//				dingdanBean tag = new dingdanBean();//查询商品 存放单个订单信息
//				tag.setDd_order(rs.getString("dd_order"));
//				tag.setDd_account(rs.getString("dd_account"));
//				tag.setDd_commodity(rs.getString("dd_commodity"));
//				tag.setDd_number(rs.getString("dd_number"));
//				tag.setTotal_price(rs.getString("total_price"));
//				tag.setStat(rs.getString("stat"));
//				tag_Array.add(tag);
//			}
//			rs.close();
//			stm.close();
//			
//			
//			} catch (SQLException e) {
//				e.printStackTrace();
//			}
//		return tag_Array;
//	}
	
	
	
	
	public static boolean getLikeListStat0(String dd_order) {//通过订单号将某个订单的状态更改并与货架商品数量运算 
		Connection conn=DBUtil.getConnectDb();//获取数据库的连接
		String sql = "UPDATE dingdan SET stat = '未出库' WHERE dd_order = ? AND stat = '已出库'";
		int rs;
		PreparedStatement stm = null;
		try {
		stm = conn.prepareStatement(sql);
		stm.setString(1,dd_order);
		
		boolean a=false;
		rs = stm.executeUpdate();
		if(rs==1){
			return true;
			} else {
			return false;
			} 
		} catch (SQLException e) {
			return false;
		}
	}
	
	
	
	
	
	
	public static ArrayList<dingdanBean> getLikeListStat1() {//查询全部未出库订单 存放多个订单信息的集合
		
		ArrayList<dingdanBean> tag_Array = new ArrayList<dingdanBean>();//建立一个数组集合
		Connection conn=DBUtil.getConnectDb();
		String sql = "select *from dingdan where stat = '已出库' ORDER BY dd_order";
		PreparedStatement stm = null;
		ResultSet rs = null;
		
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()) {//读取数据库
				dingdanBean tag = new dingdanBean();//查询商品 存放单个订单信息
				tag.setDd_order(rs.getString("dd_order"));
				tag.setDd_account(rs.getString("dd_account"));
				tag.setDd_commodity(rs.getString("dd_commodity"));
				tag.setDd_number(rs.getString("dd_number"));
				tag.setTotal_price(rs.getString("total_price"));
				tag.setStat(rs.getString("stat"));
				tag_Array.add(tag);
			}
			rs.close();
			stm.close();
			
			
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return tag_Array;
	}
	
	
	
	
//	public static ArrayList<dingdanBean> getLikeListStat1(String dd_order) {//查询全部未出库订单 存放多个订单信息的集合
//		
//		ArrayList<dingdanBean> tag_Array = new ArrayList<dingdanBean>();//建立一个数组集合
//		Connection conn=DBUtil.getConnectDb();
//		String sql = "UPDATE dingdan SET stat = '已出库' WHERE dd_order = ? AND stat = '未出库'";
//		PreparedStatement stm = null;
//		ResultSet rs = null;
//		
//		try {
//			stm = conn.prepareStatement(sql);
//			stm.setString(1,dd_order);
//			rs = stm.executeQuery();
//			while(rs.next()) {//读取数据库
//				dingdanBean tag = new dingdanBean();//查询商品 存放单个订单信息
//				tag.setDd_order(rs.getString("dd_order"));
//				tag.setDd_account(rs.getString("dd_account"));
//				tag.setDd_commodity(rs.getString("dd_commodity"));
//				tag.setDd_number(rs.getString("dd_number"));
//				tag.setTotal_price(rs.getString("total_price"));
//				tag.setStat(rs.getString("stat"));
//				tag_Array.add(tag);
//			}
//			rs.close();
//			stm.close();
//			
//			
//			} catch (SQLException e) {
//				e.printStackTrace();
//			}
//		return tag_Array;
//	}
	
	
	
	
	public static boolean getLikeListStat1(String dd_order) {//通过订单号将某个订单的状态更改并与货架商品数量运算 
		Connection conn=DBUtil.getConnectDb();//获取数据库的连接
		String sql = "UPDATE dingdan SET stat = '已出库' WHERE dd_order = ? AND stat = '未出库'";
		int rs;
		PreparedStatement stm = null;
		try {
		stm = conn.prepareStatement(sql);
		stm.setString(1,dd_order);
		
		boolean a=false;
		rs = stm.executeUpdate();
		if(rs==1){
			return true;
			} else {
			return false;
			} 
		} catch (SQLException e) {
			return false;
		}
	}
	
	
	
	
	
	
	
	
}
